#第七题防御公事
# map= [
#     [2, 2, 2, 2, 2, 2, 2, 2, 0, 1, 1, 1, 1, 1, 1, 1],
#     [2, 2, 2, 2, 2, 2, 2, 0, 0, 0, 1, 1, 1, 1, 1, 1],
#     [2, 2, 2, 2, 2, 2, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1],
#     [2, 2, 2, 2, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1],
#     [2, 2, 2, 3, 3, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1],
#     [3, 3, 3, 3, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 1],
#     [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
#     [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
# ]

def build(mp:list):
    r = len(mp)   # 地图行数
    c = len(mp[0])# 地图列数
    directions = [(1, 0), (-1, 0), (0, 1), (0, -1)]  # 分别对应 下上右左
    visited = [[False] * c for _ in range(c)]  # 建表记录是否来过
    def dfs(x,y):
        visited[x][y]=True  #走过就为True
        for x1,y1 in directions:    #分别对应行和列的改变量
            near_x,near_y=x+x1,y+y1
            if 0<=near_x<r and 0<=near_y<c and not visited[near_x][near_y]:  #边界条件
                if mp[x][y]==1 and mp[near_x][near_y]==0:
                    mp[near_x][near_y]=9    #如果当前为一隔壁为0
                elif mp[near_x][near_y]==1:
                    dfs(near_x,near_y)#走到下一个点
    #寻找开始的点
    for i in range(r):
        for j in range(c):
            if mp[i][j] == 1 and not visited[i][j]:#如果没走过且为1
                dfs(i, j)
    return mp

# print(build(map))


# [[2, 2, 2, 2, 2, 2, 2, 2, 9, 1, 1, 1, 1, 1, 1, 1],
#  [2, 2, 2, 2, 2, 2, 2, 0, 0, 9, 1, 1, 1, 1, 1, 1],
#  [2, 2, 2, 2, 2, 2, 0, 0, 0, 0, 9, 1, 1, 1, 1, 1],
#  [2, 2, 2, 2, 2, 9, 0, 0, 0, 0, 0, 9, 9, 9, 1, 1],
#  [2, 2, 2, 3, 3, 1, 9, 0, 0, 0, 0, 9, 9, 9, 1, 1],
#  [3, 3, 3, 3, 1, 1, 1, 9, 9, 9, 9, 1, 1, 1, 1, 1],
#  [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
#  [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]





